package wangwenjun.phase2.concurrencypattern.singlethreadexecution.demo01;

/**
 * @author ChangLiang
 * @date 2020/6/12
 */
public class FlightSecurityV2 {

    private int count = 0;

    //登机牌
    private String boardingPass = "null";

    //身份证
    private String idCard = "null";

    /**
     * check 方法是在pass方法中执行的， pass 方法加同步已经保证了single thread execution ，因此check 方法不需要增加同步
     * toString 方法原因与此相同
     * @param idCard
     * @param boardingPass
     */
    public synchronized void pass(String idCard, String boardingPass)
    {
        this.idCard = idCard;
        this.boardingPass = boardingPass;
        this.count++;
        check();
    }

    private void check()
    {
        if (boardingPass.charAt(0) != idCard.charAt(0)) {
            throw new RuntimeException("====Exception====" + toString());
        }
    }

    @Override
    public String toString()
    {
        return "The " + count + " passengers,boardingPass [" + boardingPass + "],idCard [" + idCard + "]";
    }
}
